package edu.thu.thss.rsa.yxy;

import java.math.BigInteger;
import java.util.Random;

public class RSATest {
	public static void elapsedTime(long startTime) {
		long stopTime = System.currentTimeMillis();
		double elapsedTime = ((double) (stopTime - startTime));
		System.out.println("Elapsed time: " + elapsedTime + " milliseconds");
	}

	public static void main(String[] args) {
		Random rnd = new Random();
		BigInteger m, m1, c;
		m = new BigInteger("1234567890987654321012345678909876543210"
				+ "1234567890987654321012345678909876543210"
				+ "1234567890987654321012345678909876543210"
				+ "1234567890987654321012345678909876543210"
				+ "1234567890987654321012345678909876543210"
				+ "1234567890987654321012345678909876543210");

		RSACryptography rsa = new RSACryptography(1024, rnd);

		// Standard RSA
		System.out.println("Message m:\n" + m + "\n");
		System.out.println("Encrypt...");
		long startTime = System.currentTimeMillis();
		c = rsa.getPublicKey().RSAEncrypt(m); // Using public key
		elapsedTime(startTime);

		System.out.println("Message encrypted with public key:\n" + c + "\n");
		System.out.println("Decrypt...");
		startTime = System.currentTimeMillis();
		m1 = rsa.getPrivateKey().RSADecrypt(c); // Using private key
		elapsedTime(startTime);
		System.out.println("Original message back, decrypted:\n" + m1 + "\n");

		// Fast RSA
		System.out.println("Message m:\n" + m + "\n");
		System.out.println("Encrypt...");
		startTime = System.currentTimeMillis();
		c = rsa.getPublicKeyFast().RSAEncrypt(m); // Using public key
		elapsedTime(startTime);

		System.out.println("Message encrypted with Fast public key:\n" + c + "\n");
		System.out.println("Decrypt...");
		startTime = System.currentTimeMillis();
		m1 = rsa.getPrivateKeyFast().RSADecrypt(c); // Using private key
		elapsedTime(startTime);
		System.out.println("Original message back, Fast decrypted:\n" + m1 + "\n");
	}
}
